﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeploymentMaxLib.Actions
{
    public class ActionMSBuild : ActionBase
    {
        /// <summary>
        /// 
        /// </summary>
        public override string LongName
        {
            get { return Common.Constants.LongName; }
        }

        public override string Description
        {
            get { return Common.Constants.Description; }
        }

        public override ActionResponse Execute(ActionRequest pActionRequest)
        {
            base.Result = Validate(pActionRequest);

            if (!base.Result.HasErrorOrCritical())
            {
                var tExecutionType =  pActionRequest.ExecutionType.ToEnum<Common.ExecutionType>();

                switch (tExecutionType)
                {
                    case Common.ExecutionType.Build:
                        {
                            var tDataObjectBuild = DataObjectBuild.FromXml(pActionRequest.DataObjectXml);
                            Build(tDataObjectBuild);
                        } break;
                }
            }

            return pActionRequest.GetResponse(this); 
        }

        public void Build(DataObjectBuild pDataObjectBuild)
        {

            var tLogFile = string.Format(".\\{0}.log", System.IO.Path.GetFileNameWithoutExtension(pDataObjectBuild.FileName));
            var tCommand = "";

            switch (pDataObjectBuild.NetFrameworkVersion)
            {
                case Common.NetFrameworkVersion.None:
                case Common.NetFrameworkVersion.v20:
                    tCommand = string.Format(Common.Constants.MSBuildCommand, Common.Constants.MSBuildExeV20, pDataObjectBuild.FileName, pDataObjectBuild.Configuration, tLogFile); break;
                case Common.NetFrameworkVersion.v35:
                    tCommand = string.Format(Common.Constants.MSBuildCommand, Common.Constants.MSBuildExeV35, pDataObjectBuild.FileName, pDataObjectBuild.Configuration, tLogFile); break;
                case Common.NetFrameworkVersion.v40:
                    tCommand = string.Format(Common.Constants.MSBuildCommand, Common.Constants.MSBuildExeV40, pDataObjectBuild.FileName, pDataObjectBuild.Configuration, tLogFile); break;
            }

            try
            {
                ///1. delete logfile if it exists
                ///2. execute command
                ///3. verify build successful

                var tErrorLog = "";
                var tOutLog = "";

                base.Result.AddResult(DeploymentMaxLib.Common.ExecuteBatch(tCommand, true, out tErrorLog, out tOutLog));

                //using (var sr = new System.IO.StreamReader(tLogFile))
                //{
                //    while (!sr.EndOfStream)
                //    {
                //        var line = sr.ReadLine();
                //        if (!line.IsEmpty())
                //        {
                //            if (System.Text.RegularExpressions.Regex.IsMatch(line, Common.Constants.VB6BuildSuccessfullRegEx))
                //            {
                //                base.Result.AddResult(new Exception(line));
                //            }
                //            break;
                //        }
                //    }
                //    sr.Close();
                //}

                System.IO.File.Delete(tLogFile);

            }
            catch (Exception exc)
            {
                base.Result.AddResult(exc);
            }
        }

        public override ActionResult Validate(ActionRequest pRequest)
        {
            throw new NotImplementedException();
        }

        public class DataObjectBuild : DataObjectBase
        {
            public string FileName { get; set; }
            public string Configuration { get; set; }
            public Common.NetFrameworkVersion NetFrameworkVersion { get; set; }

            public static DataObjectBuild FromXml(string pXml)
            {
                return (DataObjectBuild)Serializer.FromXml(pXml, typeof(DataObjectBuild));
            }
        }

        public static class Common
        {
            public enum ExecutionType
            {
                None = 0,
                Build = 1
            }

            public enum NetFrameworkVersion
            {
                None = 0,
                v20 = 1,
                v35 = 2,
                v40 = 3
            }

            public static class Constants
            {
                public const string LongName = "MSBuild";
                public const string Description = "MSBuild for .Net Framework Versions 2.0, 3.5 and 4.0";
                public const string MSBuildCommand = "\"{0}\" \"{1}\" /t:rebuild /p:configuration=\"{2}\" /verbosity:d /l:FileLogger,Microsoft.Build.Engine;logfile=\"{3}\"";
                public const string MSBuildExeV20 = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\msbuild";
                public const string MSBuildExeV35 = @"C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild";
                public const string MSBuildExeV40 = @"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild";
            }
        }
    }
}
